<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: install.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Installer extends Engine_Package_Installer_Module {
	function onInstall() {
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'user_profile_index' )->limit ( 1 );
		$page_id = $select->query ()->fetchObject ()->page_id;
		
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'vote.profile-votings' );
		
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
			$container_id = $select->query ()->fetchObject ()->content_id;
			
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_content' )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
			$middle_id = $select->query ()->fetchObject ()->content_id;
			
			$select->reset ( 'where' )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
			
			$tab_id = $select->query ()->fetchObject ();
			if ($tab_id && @$tab_id->content_id) {
				$tab_id = $tab_id->content_id;
			} else {
				$tab_id = null;
			}
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-votings', 'parent_content_id' => ($tab_id ? $tab_id : $middle_id), 'order' => 99, 'params' => '{"title":"Votings","titleCount":true}' ) );
		}
		
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'vote_profile_index' )->limit ( 1 );
		
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			$db->insert ( 'engine4_core_pages', array ('name' => 'vote_profile_index', 'displayname' => 'Voting Profile', 'title' => 'Voting Profile', 'description' => 'This is the profile for a voting.', 'custom' => 0 ) );
			$page_id = $db->lastInsertId ( 'engine4_core_pages' );
			
			// containers
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'main', 'parent_content_id' => null, 'order' => 1, 'params' => '' ) );
			$container_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $container_id, 'order' => 3, 'params' => '' ) );
			$middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'left', 'parent_content_id' => $container_id, 'order' => 1, 'params' => '' ) );
			$left_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// middle column
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'core.container-tabs', 'parent_content_id' => $middle_id, 'order' => 2, 'params' => '{"max":"10"}' ) );
			$tab_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-status', 'parent_content_id' => $middle_id, 'order' => 1, 'params' => '' ) );
			
			// left column
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-photo', 'parent_content_id' => $left_id, 'order' => 1, 'params' => '' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-options', 'parent_content_id' => $left_id, 'order' => 2, 'params' => '' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-info', 'parent_content_id' => $left_id, 'order' => 3, 'params' => '' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-populars', 'parent_content_id' => $left_id, 'order' => 5, 'params' => '{"title":"Most Popular Participants","titleCount":false}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-leaders', 'parent_content_id' => $left_id, 'order' => 4, 'params' => '{"title":"Leaders","titleCount":false}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-most-voted', 'parent_content_id' => $left_id, 'order' => 6, 'params' => '{"title":"Most Voted Participants","titleCount":false}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'kowcore.tag-cloud', 'parent_content_id' => $left_id, 'order' => 7, 'params' => '{"title":"Tag Cloud","titleCount":false}' ) );
			
			// tabs
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'activity.feed', 'parent_content_id' => $tab_id, 'order' => 1, 'params' => '{"title":"Updates"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-description', 'parent_content_id' => $tab_id, 'order' => 3, 'params' => '{"title":"Overview","titleCount":false}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'core.profile-links', 'parent_content_id' => $tab_id, 'order' => 4, 'params' => '{"title":"Links","titleCount":true}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-participants', 'parent_content_id' => $tab_id, 'order' => 5, 'params' => '{"title":"Participants","titleCount":true}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'vote.profile-participant', 'parent_content_id' => $tab_id, 'order' => 6, 'params' => '{"title":"Participation","titleCount":false}' ) );
		
		}
		
		parent::onInstall ();
	}
}